package base;
import java.io.PrintWriter;

import java.util.*;

/**
 * This class is used to run the hole coffee system
 *
 * @author ACyanA
 */
public class Gourmet_Coffee_System {
    Scanner input = new Scanner(System.in);
    private static PrintWriter stdErr = new PrintWriter(System.err, true);
    Product_Catalog catalog=new Product_Catalog();
    Store_Sales sales=new Store_Sales();
    Order cur_order=new Order();
    Product pro_1=new Product("01","very good",10.0,1);
    Product pro_2=new Product("02","looks very good",50.0,2);
    Product pro_3=new Product("03","excellent",25.0,3);
    /**
     * display catalog
     */
    public void DisplayCatalog() {
        catalog.showAllItems();
    }

    /**
     * display product
     */
    public void DisplayProduct() {
        System.out.println("Please input the Product code:");
        String x=input.next();
        try {
            catalog.getProduct(x).showProduct();
        }
        catch(Exception e){
            System.out.println("This Product does not exist!");
        }
    }

    /**
     * display current order
     */
    public void DisplayCurOrder() {
        if(cur_order.empty())System.out.println("The current order is empty.");
        else cur_order.showOrder();
    }

    /**
     * add product to current order
     */
    public void ModifyProductInOrder() {
        String x;
        System.out.println("Please input the Product code:");
        x = input.next();
        try {
            Product pro_cur=catalog.getProduct(x);
            if(pro_cur==null)System.out.println("This Product does not exist!");
            cur_order.addItem(pro_cur);
        }
        catch (Exception e){
            System.out.println("This Product does not exist!");
        }
    }

    /**
     * del product from current order
     */
    public void RemoveProductInOrder() {
        String x;
        System.out.println("Please input the Product code:");
        x=input.next();
        try {
            Product pro_cur=catalog.getProduct(x);
            if(pro_cur==null)System.out.println("This Product does not exist!");
            cur_order.delItem(pro_cur);
        }
        catch (Exception e){
            System.out.println("This Product does not exist!");
        }
    }

    /**
     * add current to sales and then reset current order
     */
    public void RegisterSale() {
        if(cur_order.empty()) {
            System.out.println("The current order is empty.");
        }
        else {
            sales.addOrder(cur_order);
            cur_order = new Order();
        }
    }

    /**
     * display sales
     */
    public void DisplaySales() {
        sales.showSales();
    }

    /**
     * display number of orders with a specific product
     */
    public void DisplayNumOrderOfProduct() {
        String x;
        int ans=0;
        System.out.println("Please input the ProductName:");
        x=input.next();
        try {
            for (Iterator<Order> i = sales.getOrdersIterator(); i.hasNext(); ) {
                Order ord = i.next();
                if (ord.getItem(x) != null) ans++;
            }
            System.out.println("There are " + ans + " orders include this product");
        }
        catch (Exception e){
            System.out.println("This Product does not exist!");
        }
    }

    /**
     * display the total quantity sold for each product choice
     */
    public void DisplayQuantityOfProduct() {
        int Num=0;
        for (Product P:catalog.Products)
        {
            int ans=0;
            for (Iterator<Order> i = sales.getOrdersIterator(); i.hasNext();)
            {
                Order ord = i.next();
                if(ord.getItem(P.getCode())!=null)ans++;
            }
            System.out.println("There are "+ans+" orders include product"+(++Num));
        }

    }

    /**
     * !!!
     * the entry of this program
     */
    public void Run() {

        catalog.addProduct(pro_1);
        catalog.addProduct(pro_2);
        catalog.addProduct(pro_3);
        while(true) {
            System.out.println("[0] Quit");
            System.out.println("[1] Display catalog");
            System.out.println("[2] Display product");
            System.out.println("[3] Display current order");
            System.out.println("[4] Add product to|in current order");
            System.out.println("[5] Remove product from current order");
            System.out.println("[6] Register sale of current order");
            System.out.println("[7] Display sales");
            System.out.println("[8] Display number of orders with a specific product");
            System.out.println("[9] Display the total quantity sold for each product choice");
            System.out.println("choice> ");
            try {
                int x = input.nextInt();
                if (x == 0) System.exit(0);
                else if (x == 1) DisplayCatalog();
                else if (x == 2) DisplayProduct();
                else if (x == 3) DisplayCurOrder();
                else if (x == 4) ModifyProductInOrder();
                else if (x == 5) RemoveProductInOrder();
                else if (x == 6) RegisterSale();
                else if (x == 7) DisplaySales();
                else if (x == 8) DisplayNumOrderOfProduct();
                else if (x == 9) DisplayQuantityOfProduct();
            }
            catch (NumberFormatException nfe) {
                stdErr.println(nfe);
                stdErr.flush();
            }
        }
    }
}